package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.Arc;
import com.efast.dmom.aps.bean.ProductionLine;
import com.efast.dmom.aps.bean.Route;
import com.efast.dmom.aps.bean.RouteQueue;
import com.efast.dmom.aps.bean.RouteStep;
import com.efast.dmom.aps.dao.RouteDao;
import com.efast.dmom.aps.entity.RouteEntity;
import com.efast.dmom.aps.entity.RouteQueueEntity;
import com.efast.dmom.aps.entity.base.IModificationFlags;
import com.efast.dmom.aps.util.CommonConstants;
import com.efast.dmom.aps.util.TimeUtil;

/**
 * @author Aaron Liu
 *
 * @date:2020年2月4日
 */
@Component
public class RouteDataHandler {

	@Autowired
	private RouteStepDataHandler routeStepDataHander = null;

	@Autowired
	private RouteArcDataHandler acrDataHandler = null;

	@Autowired
	private RouteQueueDataHandler routeQueueDataHandler = null;

	@Autowired
	private RouteDao routeDao = null;

	@Autowired
	private RouteProdLineRelDataHandler rPlineRelDataHandler = null;

	public RouteDataHandler() {
		// TODO Auto-generated constructor stub
	}

	public void save(Route target) {

		if (target != null) {

			RouteEntity routeEntity = (RouteEntity) target.getInnerData();

			List<ProductionLine> lineList = target.getProductionLines();
			long defaultLineKey = target.getInnerData().getDefaultPlineKey();

			if (IModificationFlags.UNCHANGED == routeEntity.getModificationFlag()) {
				return;
			}

			else if (IModificationFlags.NEW == routeEntity.getModificationFlag()) {
				
				routeEntity.setSiteId(DataHandlerUtils.getSiteId());
				routeEntity.setCreationTime(TimeUtil.getLocalDateTime());
				routeEntity.setCreationTimeZone(TimeUtil.getLocalTimeZone());
				routeEntity.setCreator(SpringWebUtils.getRequestUser());
				routeEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				routeEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
				routeEntity.setLastModifier(SpringWebUtils.getRequestUser());
				// routeEntity.setCreationPid(DataHandlerUtils.getDmomPid());
				// routeEntity.setLastModifiedPid(DataHandlerUtils.getDmomPid());
				routeEntity.setRowStatus(1);
				routeEntity.setRowTrxId(1);
				routeDao.saveRouteObj(routeEntity);
				rPlineRelDataHandler.saveRouteProdLineRel(routeEntity.getId(), routeEntity.getUuid(),
						routeEntity.getDefaultProdlineKey(), lineList);
			} 
			else if (IModificationFlags.MODIFIED == routeEntity.getModificationFlag()) {

				if (routeEntity.getId() <= 0) {
					return;
				}
				routeEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				routeEntity.setLastModifier(SpringWebUtils.getRequestUser());
				routeEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
				// routeEntity.setLastModifiedPid(DataHandlerUtils.getDmomPid());
				routeDao.updateRouteObj(routeEntity);

				if (target.getProductionLines() != null && target.getProductionLines().size() > 0) {
					rPlineRelDataHandler.removeRelByRouteId(routeEntity.getId());
					rPlineRelDataHandler.saveRouteProdLineRel(routeEntity.getId(), routeEntity.getUuid(),
							defaultLineKey, lineList);
				}
			}

			if (IModificationFlags.NEW == routeEntity.getModificationFlag()
					|| IModificationFlags.MODIFIED == routeEntity.getModificationFlag()) {
				saveContainedObjects(target, routeEntity.getModificationFlag());
			}
		}
	}
	
	private void saveContainedObjects(Route target, int flag) {

		List<RouteStep> stepList = target.getRouteSteps();
		List<RouteQueue> queueList = target.getRouteQueues();

		target.setRouteQueues(routeQueueDataHandler.saveQueueList(target, queueList));
		target.setRouteSteps(routeStepDataHander.saveRouteSteps(target, stepList));

		List<Arc> arcList = target.getRouteArcs();
		List<Arc> newArcList = new ArrayList<Arc>();
		List<Arc> deletedArcList = new ArrayList<Arc>();

		for (Arc arc : arcList) {

			if (arc.getInnerData().getModificationFlag() == IModificationFlags.DELETED) {
				deletedArcList.add(arc);
			}

			if (arc.getInnerData().getModificationFlag() == IModificationFlags.NEW) {
				newArcList.add(arc);
			}
		}

		if (newArcList.size() > 0) {
			acrDataHandler.saveArc(target, newArcList);
		}

		if (deletedArcList.size() > 0) {
			acrDataHandler.removeArcs(deletedArcList);
		}
	}

	public List<RouteStep> addRouteStep(Route route, List<RouteStep> stepList) {

		return routeStepDataHander.saveRouteSteps(route, stepList);
	}

	public void removeRouteObjById(long routeId) {

		if (routeId <= 0) {
			return;
		}
		acrDataHandler.removeArcsByRouteId(routeId);
		routeStepDataHander.removeStepsByRouteId(routeId);
		rPlineRelDataHandler.removeRelByRouteId(routeId);
		routeQueueDataHandler.removeQueueByRouteId(routeId);
		routeDao.removeRouteObjById(routeId);
	}

	public boolean isExist(String name) {
		return routeDao.getRouteByName(name) == null ? false : true;
	}

	public Route createRoute(String name) {

		Route route = new Route();

		List<RouteQueue> queueList = new ArrayList<RouteQueue>();

		RouteEntity routeEntity = new RouteEntity(name);

		RouteQueueEntity entryQueueEntity = new RouteQueueEntity();
		entryQueueEntity.setAutoStart(false);
		entryQueueEntity.setQueueType(CommonConstants.ROUTE_QUEUE_TYPE_ENTRY);
		entryQueueEntity.setName(CommonConstants.ROUTE_ENTRY_QUEUE_NAME);
		entryQueueEntity.setModificationFlag(IModificationFlags.UNCHANGED);

		RouteQueue entryQueue = new RouteQueue();
		entryQueue.setInnerData(entryQueueEntity);

		RouteQueueEntity exitQueueEntity = new RouteQueueEntity();
		exitQueueEntity.setAutoStart(false);
		exitQueueEntity.setQueueType(CommonConstants.ROUTE_QUEUE_TYPE_EXIT);
		exitQueueEntity.setName(CommonConstants.ROUTE_EXIT_QUEUE_NAME);
		exitQueueEntity.setModificationFlag(IModificationFlags.UNCHANGED);

		RouteQueue exitQueue = new RouteQueue();
		exitQueue.setInnerData(exitQueueEntity);

		queueList.add(entryQueue);
		queueList.add(exitQueue);

		routeEntity.setModificationFlag(IModificationFlags.UNCHANGED);
		route.setInnerData(routeEntity);
		route.setRouteQueues(queueList);

		return route;
	}

}
